package au.edu.unsw.bookmarket;

import au.edu.unsw.bookmarket.dao.BookDao;
import au.edu.unsw.bookmarket.dao.CourseDao;
import au.edu.unsw.bookmarket.dao.impl.BookDaoImpl;
import au.edu.unsw.bookmarket.dao.impl.CourseDaoImpl;
import au.edu.unsw.bookmarket.jdo.*;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;

/**
 * Hello world!
 *
 */
public class DataInsertion {

    public static void main(String[] args) {
        System.out.println("Hello World!");
        DataInsertion dataInsertion = new DataInsertion();
        dataInsertion.readFile("postgrad_courses.txt");
    }

    public void readFile(String filename) {
        File file = new File(filename);
        BufferedReader reader = null;
        BookDao bookDao = new BookDaoImpl();
        CourseDao courseDao = new CourseDaoImpl();
        try {
            reader = new BufferedReader(new FileReader(file));
            if (filename.equals("postgrad_courses.txt")) {
                processingCourse(reader, courseDao);
            } else if (filename.equals("postgrad_books.txt")) {
                processingTextbook(reader, bookDao);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public void processingCourse(BufferedReader reader, CourseDao courseDao) {
        try {
            StringBuilder contents = new StringBuilder();
            String text = null;
            while ((text = reader.readLine()) != null) {
                contents.append(text).append(System.getProperty("line.separator"));
                if (text.trim().equals("")) {
                    continue;
                }
                String[] tuple = text.split(",", -2);
                if (!tuple[0].trim().equals("")) {
                    for (String s : tuple) {
                        System.out.println(s);
                    }
                    Course course = new Course(tuple[0], tuple[3], tuple[2], tuple[1], "", "", true);
                    courseDao.addCourse(course);
                }
            }
//            System.out.println(contents.toString());
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void processingTextbook(BufferedReader reader, BookDao bookDao) {
        try {
            StringBuilder contents = new StringBuilder();
            String text = null;
            while ((text = reader.readLine()) != null) {
                contents.append(text).append(System.getProperty("line.separator"));
                if (text.trim().equals("")) {
                    continue;
                }
                String[] tuple = text.split(",", -2);
                if (!tuple[1].trim().equals("")) {
                    for (String s : tuple) {
                        System.out.println(s);
                    }
                    Book book = new Book(tuple[1], tuple[6], tuple[8], 0, tuple[4], tuple[7], tuple[3], Double.parseDouble(tuple[5]), true);
                    System.out.println(book.getName());
                    System.out.println(book.getAuthor());
                    System.out.println(book.getEdition());
                    System.out.println(book.getYear());
                    System.out.println(book.getIsbn());
                    System.out.println(book.getPublisher());
                    System.out.println(book.getType());
                    System.out.println(book.getFull_price());
                    bookDao.addBook(book);
                }
            }
            System.out.println(contents.toString());
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void insertData() {
    }
}
